package games;

import games.KamisadoState.KamisadoMove;

import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.util.HashMap;

public class KamisadoGUI {
	
	private HashMap<SquareColor, Color> colorTranslation = new HashMap<SquareColor, Color>();
	private Controller controller;
	private JPanel gridLayout;
	private JFrame frame;
	
	public KamisadoGUI(Controller controller) {
		colorTranslation.put(SquareColor.BLUE, Color.BLUE);
		colorTranslation.put(SquareColor.RED, Color.RED);
		colorTranslation.put(SquareColor.BROWN, Color.BLACK);
		colorTranslation.put(SquareColor.GREEN, Color.GREEN);
		colorTranslation.put(SquareColor.ORANGE, Color.ORANGE);
		colorTranslation.put(SquareColor.PINK, Color.PINK);
		colorTranslation.put(SquareColor.PURPLE, Color.MAGENTA);
		colorTranslation.put(SquareColor.YELLOW, Color.YELLOW);
		
		this.controller = controller;
		
		//Create and set up the window.
		this.frame = new JFrame("Kamisado");
        this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        this.gridLayout = new JPanel();
        this.gridLayout.setLayout(new GridLayout(8,8));
	}
	
	public void createAndShowGUI(KamisadoState state) {
        this.gridLayout.removeAll();
		KamisadoMove km = state.getPreviousMove();
        for(int y = 7; y >= 0; y--) {
        	for(int x = 0; x < 8; x++) {
        		KamisadoSquare ks = state.cell(x, y);
        		JPanel jpanel = new JPanel();
        		jpanel.setBackground(colorTranslation.get(ks.getColor()));
        		jpanel.setOpaque(true);
        		jpanel.addMouseListener(new HandleMouseEvent(x,y, this.controller));
        		if(ks.getTower() != null) {
        			JPanel tower = new JPanel();
        			if(km != null && km.getX() == x && km.getY() == y)
        				tower.setPreferredSize(new Dimension(50, 50));
        			else
        				tower.setPreferredSize(new Dimension(30, 30));
        			tower.setBackground(colorTranslation.get(ks.getTower().getColor()));
        			tower.setBorder(new LineBorder(Color.WHITE));
        			
        			JLabel playerMark;
        			if(!ks.getTower().getPlayer())
        				playerMark = new JLabel("1");
        			else
        				playerMark = new JLabel("2");
        			
        			if(ks.getTower().getColor() == SquareColor.BROWN)
        				playerMark.setForeground(Color.WHITE);
        			
        			tower.add(playerMark);
        			jpanel.add(tower);
        		}
        		
        		this.gridLayout.add(jpanel);
        	}
        }
        
        this.frame.getContentPane().add(gridLayout);
        
        //Display the window.
        //frame.pack();
        int x = frame.getX();
        int y = frame.getY();
        if(x == 0)
        	x = 50;
        if(y == 0 || y == 22)
        	y = 50;
        
        Dimension size = frame.getSize();
        if(size.height == 0)
        	frame.setBounds(x, y, 580, 580);
        else
        	frame.setBounds(x, y, size.width, size.height);
        
        frame.setVisible(true);
    }
}
